#include "tree.h"

void CreateBiT(Nodeptr *T,char *p){
	if(*p == '#')
	*T = NULL;
	else
	{
		*T = (Node *)malloc(sizeof(Node));
		(*T)->data = *p;
		CreateBiT(&(*T)->Lnode,++p);
		CreateBiT(&(*T)->Rnode,++p);
	}
}

void PrintBiT(Nodeptr *T){
	if(*T)
	{
		PrintBiT(&(*T)->Lnode);
		printf("%c ",(*T)->data);
		PrintBiT(&(*T)->Rnode);
	}
}

void CopyTree(Node *T,Node *newT){
	if(T == NULL)
	{
		newT = NULL;
		return;
	}
	else
	{
		newT = (Node *)malloc(sizeof(Node));
		newT->data = T->data;
		CopyTree(T->Lnode,newT->Lnode);
		CopyTree(T->Rnode,newT->Rnode);
	}
}

int Depth(Node *T){
	int m = 0;
	int n = 0;

	if(T == NULL)
	return 0;
	else
	{
		m = Depth(T->Lnode);
		n = Depth(T->Rnode);
		if(m>n)
		return m+1;
		return n+1;		
	}	
}

int NodeCount(Node *T){
	if(T == NULL)
	return 0;
	else
	return NodeCount(T->Lnode)+NodeCount(T->Rnode)+1;
}

void destroy(Node *T){
	if(T)
	{
		if(T->Lnode)
		destroy(T->Lnode);
		if(T->Rnode)
		destroy(T->Rnode);
		free(T);
		T = NULL;
	}
}

